package org.colin.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringEscapeUtils;
import org.colin.aop.annotation.LogAnnotation;
import org.colin.code.BaseResponseCode;
import org.colin.config.ConfigCode;
import org.colin.dictPojo.DEntry;
import org.colin.dictPojo.DSubject;
import org.colin.dictPojo.DSubjectEnh;
import org.colin.service.DEntryService;
import org.colin.service.DSubjectService;
import org.colin.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 题目表 前端控制器
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Controller
@Api(tags = "基础数据管理-题库管理")
@RequestMapping("/dSubject")
public class DSubjectController {




    @Autowired
    DSubjectService dSubjectService;
    @Autowired
    DEntryService dEntryService;


    @ApiOperation(value = "新增题库接口")
    @LogAnnotation(title = "基础数据管理", action = "新增题库接口")
    @PostMapping("addSubject")
    @ResponseBody
    public DataResult addSubject(@ApiParam(name="题库类",value="题库对象",required=true)@RequestBody @Valid  DSubjectEnh dSubject){
        Integer language = dSubject.getLanguage();
        if(language == null || language.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败,语种不能为空");
        }
        Integer type = dSubject.getType();
        if(type == null || type <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败,题目类型不能为空");
        }

        Integer hsk = dSubject.getHsk();
        if(hsk == null || hsk <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "保存失败,题目等级不能为空");
        }
        String entryName = dSubject.getEntryName();
        if(entryName == null || entryName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "保存失败,词条不能为空");
        }
        String stem = dSubject.getStem();
        if(stem == null || stem.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "保存失败,题干不能为空");
        }
        dSubject.setCreateTime(LocalDateTime.now());

        String videoLists = dSubject.getVideoLists();
        List videoList = null;
        if(videoLists != null && !videoLists.trim().equals("")){
            videoList = JSONArray.parseArray(videoLists);
        }
        String imageLists = dSubject.getImageLists();
        List imageList = null;
        if(imageLists != null && !imageLists.trim().equals("")){
            imageList = JSONArray.parseArray(imageLists);
        }
        String recordingLists = dSubject.getRecordingList();
        List recordingList = null;
        if(recordingLists != null && !recordingLists.trim().equals("")){
            recordingList = JSONArray.parseArray(recordingLists);
        }
        String chooseLists = dSubject.getChooseLists();
        List chooseList = null;
        if(chooseLists != null && !chooseLists.trim().equals("")){
            chooseList = JSONArray.parseArray(chooseLists);
        }
        DSubject dSubject1 = JSONObject.parseObject(JSONObject.toJSONString(dSubject)).toJavaObject(DSubject.class);

        boolean save = dSubjectService.addSubject(dSubject1, videoList , imageList , recordingList,chooseList);
        if(save == true){
           return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败");
        }
    }


    @ApiOperation(value = "修改题库接口")
    @PostMapping("updateSubject")
    @LogAnnotation(title = "基础数据管理", action = "修改题库接口")

    @ResponseBody
    public DataResult updateSubject(@ApiParam(name="题库类",value="题库对象",required=true)@RequestBody @Valid  DSubjectEnh dSubject ){


//        videoLists=dSubject.getVideoLists();
// imageLists=dSubject.getImageLists();
// recordingLists=dSubject.getRecordingList();
        Integer id = dSubject.getId();
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,唯一标识不能为空");
        }
        Integer levelName = dSubject.getLanguage();
        if(levelName == null || levelName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,语种不能为空");
        }

        Integer type = dSubject.getType();
        if(type == null || type <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,题目类型不能为空");
        }

        Integer hsk = dSubject.getHsk();
        if(hsk == null || hsk <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,题目等级不能为空");
        }
        String entryName = dSubject.getEntryName();
        if(entryName == null || entryName.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,词条不能为空");
        }
        String stem = dSubject.getStem();
        if(stem == null || stem.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,题干不能为空");
        }
        String imageLists = dSubject.getImageLists();
        List imageList = null;
        if(imageLists != null && !imageLists.trim().equals("")){
            imageList = JSONArray.parseArray( imageLists);
        }
        String videoLists = dSubject.getVideoLists();
        List videoList = null;
        if(videoLists != null && !videoLists.trim().equals("")){
            videoList = JSONArray.parseArray(videoLists);
        }

        String recordingLists = dSubject.getRecordingList();
        List recordingList = null;
        if(recordingLists != null && !recordingLists.trim().equals("")){
            recordingList = JSONArray.parseArray(recordingLists);
        }
        String chooseLists = dSubject.getChooseLists();
        List chooseList = null;
        if(chooseLists != null && !chooseLists.trim().equals("")){
            chooseList = JSONArray.parseArray(chooseLists);
        }
        DSubject dSubject1 = JSONObject.parseObject(JSONObject.toJSONString(dSubject)).toJavaObject(DSubject.class);
        boolean save = dSubjectService.updateSubject(dSubject1, videoList , imageList , recordingList,chooseList);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败");
        }
    }



    @ApiOperation(value = "批量删除题库接口")
    @LogAnnotation(title = "基础数据管理", action = "批量   删除题库接口")
    @DeleteMapping("deleteSubject")
    @ResponseBody
    public DataResult deleteBatchSubject(@ApiParam(name="id",value="[1,2,3]",required=true)@RequestBody String id){
        List ids = JSONArray.parseArray(id);
        if(ids == null || ids.size() <=0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"删除失败,唯一标识不能为空");
        }

        Integer save = dSubjectService.deleteBatchSubject(ids);
        if(save == 1){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"删除失败");
        }
    }


    @ApiOperation(value = "查询题库详情")
    @LogAnnotation(title = "基础数据管理", action = "查询题库详情")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "唯一标识", name = "id" , paramType = "query",required=true),
    })
    @GetMapping("selectSubjectById")
    @ResponseBody
    public  DataResult selectSubjectById(Integer id){
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "查询失败,唯一标识不能为空");
        }
        HashMap byId = dSubjectService.selectSubjectById(id);
        DataResult success = DataResult.success();
        success.setData(byId);
        return success;
    }


    @ApiOperation(value = "查询题库-分页")
    @LogAnnotation(title = "基础数据管理", action = "查询题库-分页")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "id或词头", name = "param" , paramType = "query"),
            @ApiImplicitParam(value = "试题标签", name = "type" , paramType = "query"),
            @ApiImplicitParam(value = "语种", name = "language" , paramType = "query"),
            @ApiImplicitParam(value = "类型", name = "convType" , paramType = "query"),
            @ApiImplicitParam(name="page",value="当前页" ,required=true, paramType = "query"),
            @ApiImplicitParam(name="pageSize",value="每页数量" ,required=true, paramType = "query")
    })
    @GetMapping("selectSubjectIsPage")
    @ResponseBody
    public DataResult selectSubjectIsPage(String param ,Integer type ,Integer language ,String convType ,Integer page , Integer pageSize ){
        Page<HashMap> pageD = new Page<>(page, pageSize);
        IPage<HashMap> list = dSubjectService.selectSubjectIsPage(pageD , param , type , language , convType);
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }


}

